package com.lgd.warehousemanagement.Broadcast;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Handler;
import android.util.Log;
import android.widget.Toast;

import com.lgd.warehousemanagement.Activity.MainActivity;
import com.lgd.warehousemanagement.Utils.ToastUtils;

import java.io.IOException;


/**
 * @Author lgd
 * @create 2024/5/30 9:09
 *   自启动，网络监测 广播
 */
public class MyReceiver extends BroadcastReceiver {

    final static String ACTION = "android.intent.action.BOOT_COMPLETED";
    private static final String TAG = "MyReceiver";
    @Override
    public void onReceive(Context context, Intent intent) {
        //  开机自启
        if(intent.getAction().equals(ACTION)){
            Intent openIntent = new Intent(context, MainActivity.class);
            openIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            Handler handler = new Handler();
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    context.startActivity(openIntent);
                }
            },2000);
        }
        //  判断 WiFi 连接状态
        if (intent.getAction().equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) {
            NetworkInfo info = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
            if (info.getState().equals(NetworkInfo.State.CONNECTED)) {
                WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
                WifiInfo wifiInfo = wifiManager.getConnectionInfo();
                Log.d("连接状态", "连接到WIFI " + wifiInfo);
                if (!wifiInfo.getSSID().equals("<unknown ssid>")){
                    ToastUtils.successShowLong(context,"连接到WiFi"+wifiInfo.getSSID());
                }
            }
        }
        if (intent.getAction().equals(WifiManager.WIFI_STATE_CHANGED_ACTION)) {
            int wifistate = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, WifiManager.WIFI_STATE_DISABLED);
            if (wifistate == WifiManager.WIFI_STATE_DISABLED) {
                Log.d("连接状态", "WIFI关闭");
                ToastUtils.successShowLong(context,"WiFi关闭");
            }
            if (wifistate == WifiManager.WIFI_STATE_ENABLED) {
                Log.d("连接状态", "WIFI开启");
                ToastUtils.successShowLong(context,"WiFi开启");
            }
        }

    }

    public void NetStates(Context context ,Intent intent){
        //网络广播接收者
        if (intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
            ConnectivityManager connectivityManager = (ConnectivityManager)
                    context.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo activeInfo = connectivityManager.getActiveNetworkInfo();
            NetworkInfo netInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
            NetworkInfo wifiInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
            if (activeInfo != null) {
                //网络可用
                if (activeInfo.getState().equals(NetworkInfo.State.CONNECTED)) {
                    Log.d(TAG, "NetStates: 可用");
                    //判断移动数据
                    if (netInfo.getState().equals(NetworkInfo.State.CONNECTED)) {
                        Toast.makeText(context, "您正在使用移动数据", Toast.LENGTH_SHORT).show();
                    }
                    //判斷Wifi數據
                    if (wifiInfo.getState().equals(NetworkInfo.State.CONNECTED)) {
                        Toast.makeText(context, "您正在使用Wifi数据", Toast.LENGTH_SHORT).show();
                        Log.d(TAG, "NetStates: -----您正在使用Wifi数据---------");
                    }
                } else {
                    Toast.makeText(context, "请检查网络是否已联网", Toast.LENGTH_SHORT).show();
                    Log.d(TAG, "NetStates: 不可用");
                }
            }
        }

    }

    public static class NetworkDiagnostics {
        //方案一
        public static void start(final Context context){
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        Process process = Runtime.getRuntime().exec("/system/bin/ping -c 1 -w 100 www.baidu.com");
                        int status = process.waitFor(); //如果status==0则表示网络可用，其中参数-c 1是指ping的次数为1次，-w是指超时时间单位为s
                        if (status == 0){     //网络能通
                            Log.d("wangrui","网络分析方案1=true");
                            System.out.println("-----可用------");
                        }else{
                            Log.d("wangrui","网络分析方案1=false");
                        }
                    } catch (IOException | InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }

    }

}
